<?php
	namespace Admin\Controller;
	use Tools\AdminController;	
class IntegralGoodsController extends AdminController {
    public function goalstore(){
		//实现数据分页效果
		//$goods=new \Model\GoodsModel();				
    	$goods=new \Model\IntegralGoodsModel();
		//获取数据总条数
		$total=$goods->count();
		$per=12;
		//实例化分页类对象
		$page_obj=new \Tools\CssPage($total,$per);
		$sql="select * from integralgoods order by id desc ".$page_obj->limit;
		$info=$goods->query($sql);
		//$info=$goods->limit('7')->order('goods_id desc')->select();
		//获得页码列表
		//dump($info);
		$pagelist=$page_obj->fpage(array(3,4,5,6,7,8));
		$this->assign('total',$total);
		$this->assign('pagelist',$pagelist);
		$this->assign('info',$info);
		$this->display();
    }
	public function cats(){
		$cats_model=new \Model\CatsModel();
		$sql="select s.*, p.title as parent_title from goods_cats s left join goods_cats p on s.parent_uid=p.uid;";
		$cats=$cats_model->query($sql);
		$this->assign('cats',$cats);
		$this->display();
	}
	public function addcats(){
		$cats=new \Model\CatsModel();
			if(!empty($_POST)){
				//var_dump($_POST);
				//上传商品图片
				$cfg=array('rootPath'      => './upload/');
				$up=new \Think\Upload($cfg);
				$upload_result=$up->uploadOne($_FILES['image']);
				//图片路径
				$pic_path=$up->rootPath.$upload_result['savepath'].$upload_result['savename'];
				//修改为新的图片路径
				$_POST['image']=ltrim($pic_path,'.');
				$_POST['create_time']=$_SERVER['REQUEST_TIME'];
				if($cats->add($_POST)){
					$this->success('Index/success');
					$this->redirect('IntegralGoods/cats',array(),3," ");
				}else{
					$this->error('Index/error');
					$this->redirect('IntegralGoods/cats',array(),3," ");
				}
				
			}else{
				$cats_info=$cats->select();
				$this->assign('cats_info',$cats_info);
				$this->display();
			}
	}
	public function editcats($uid){
//			var_dump($_POST);var_dump($_FILES);
		$cats=new \Model\CatsModel();
			if(!empty($_POST)){
				//var_dump($_POST);
				//上传商品图片
				$cfg=array('rootPath'      => './upload/');
				$up=new \Think\Upload($cfg);
				if($_FILES['image']['error']!=4){//判断是否上传了图片,不上传则不修改图片
				
					$upload_result=$up->uploadOne($_FILES['image']);
					//图片路径
					$pic_path=$up->rootPath.$upload_result['savepath'].$upload_result['savename'];
					//修改为新的图片路径
					$_POST['image']=ltrim($pic_path,'.');
				}
					$_POST['create_time']=$_SERVER['REQUEST_TIME'];
//					var_dump($_POST);
					if($cats->save($_POST)){//成功
						$this->success('Index/success');
						$this->redirect('IntegralGoods/cats',array(),3," ");
					}else{//失败
						$this->error('Index/error');
						$this->redirect('IntegralGoods/cats',array(),3,' ');
					}
			}else{
				$info=$cats->where("uid='$uid'")->find();
				//var_dump($info);
				$cats_info=$cats->select();
				$this->assign('cats_info',$cats_info);
				$this->assign("info",$info);
				$this->display();
			}
	}
	public function deletecats($uid){
		$cats=new \Model\CatsModel();
		$sql="delete from goods_cats where uid=$uid;";
		if($cats->execute($sql)){
			//$this->redirect('IntegralGoods/goalstore',array(),3);
			$this->success('Index/success');
			$this->redirect('IntegralGoods/cats',array(),3," ");
		}else{
			//$this->redirect('IntegralGoods/goalstore',array(),3,'success!back after 3s ');
			$this->error('Index/error');
			$this->redirect('IntegralGoods/cats',array(),3,' ');
		}
	}
	public function editintegralgoods($id){
//		var_dump($_POST);var_dump($_FILES);
			$goods=new \Model\IntegralGoodsModel();
			if(!empty($_POST)){
				//var_dump($_POST);
				//上传商品图片
				$cfg=array('rootPath'      => './upload/');
				$up=new \Think\Upload($cfg);
				if($_FILES['main_img']['error']!=4){//判断是否上传了图片,不上传则不修改图片
				
					$upload_result=$up->uploadOne($_FILES['main_img']);
					//图片路径
					$pic_path=$up->rootPath.$upload_result['savepath'].$upload_result['savename'];
					//修改为新的图片路径
					$_POST['main_img']=ltrim($pic_path,'.');
				}
				$_POST['modify_time']=$_SERVER['REQUEST_TIME'];
					if($goods->save($_POST)){//成功
						$this->success('Index/success');
						$this->redirect('IntegralGoods/goalstore',array(),3," ");
					}else{//失败
						$this->error('Index/error');
						$this->redirect('IntegralGoods/goalstore',array(),3,' ');
					}
			}else{
				$info=$goods->where("id='$id'")->find();
				//var_dump($info);
				$cats=D('goods_cats')->select();
				$this->assign('cats',$cats);
				$this->assign("info",$info);
				$this->display();
			}
	}
	public function deleteintegralgoods($id){
		$goods=new \Model\IntegralGoodsModel();
		$sql="delete from integralgoods where id=$id;";
		if($goods->execute($sql)){
			//$this->redirect('IntegralGoods/goalstore',array(),3);
			$this->success('Index/success');
			$this->redirect('IntegralGoods/goalstore',array(),3," ");
		}else{
			//$this->redirect('IntegralGoods/goalstore',array(),3,'success!back after 3s ');
			$this->error('Index/error');
			$this->redirect('IntegralGoods/goalstore',array(),3,' ');
		}
	}
	public function addintegralgoods(){
		//var_dump($_POST);var_dump($_FILES);
		$goods=new \Model\IntegralGoodsModel();
			if(!empty($_POST)){
				//var_dump($_POST);
				//上传商品图片
				$cfg=array('rootPath'      => './upload/');
				$up=new \Think\Upload($cfg);
				$upload_result=$up->uploadOne($_FILES['main_img']);
				//图片路径
				$pic_path=$up->rootPath.$upload_result['savepath'].$upload_result['savename'];
				//修改为新的图片路径
				$_POST['main_img']=ltrim($pic_path,'.');
				$_POST['create_time']=$_SERVER['REQUEST_TIME'];
				if($goods->add($_POST)){
					$this->success('Index/success');
					$this->redirect('IntegralGoods/goalstore',array(),3," ");
				}else{
					$this->error('Index/error');
					$this->redirect('IntegralGoods/goalstore',array(),3," ");
				}
				
			}else{
				$cats=D('goods_cats')->select();
				$this->assign('cats',$cats);
				$this->display();
			}
	}
	function comments(){
		//分页相关
		$total=D('goods_comment')->count();
		$per=10;
		//实例化分页类对象
		$page_obj=new \Tools\CssPage($total,$per);
		$sql="select c.*,i.name as goods_name,u.name as user_name from goods_comment as c,integralgoods as i,user as u where c.goods_uid=i.id and c.user_uid=u.id order by c.create_time desc ".$page_obj->limit;
		$comments=D('goods_comment')->query($sql);
//		var_dump($comments);
		$pagelist=$page_obj->fpage(array(3,4,5,6,7,8));
		$this->assign('pagelist',$pagelist);
		$this->assign('total',$total);
		$this->assign('comments',$comments);
		$this->display();
	}
	function changecommentsstatus($uid,$status){
		$db=new \Model\DbModel();
		$res=$db->updatedataforint("goods_comment",array("status"=>$status?0:1),"uid=".$uid);
		if($res){
			$this->ajaxReturn(array("errorcode"=>0,"error"=>"成功"));
		}else{
			$this->ajaxReturn(array("errorcode"=>1,"error"=>"未知错误"));
		}
	}
}